<?php

namespace app\admin\controller\bill;

use app\common\controller\Backend;
use app\common\model\User as FUser;
use think\Db;

/**
 * 分账明细管理
 *
 * @icon fa fa-circle-o
 */
class Bill extends Backend
{

    /**
     * Bill模型对象
     * @var \app\admin\model\bill\Bill
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\bill\Bill;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("couponStatusList", $this->model->getCouponStatusList());
        $this->view->assign("comfirmStatusList", $this->model->getComfirmStatusList());
        $this->view->assign("goodsTypeList", $this->model->getGoodsTypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $subTotalMoney = 0; //分账总金额
            $therMoney = 0; //已经分账金额
            $noMoney = 0; //未分账金额
            $subList = $this->model->where($where)->select();
            if($subList){
                foreach ($subList as $v){
                    $subTotalMoney = bcadd($subTotalMoney,$v['fz_money'],2);//分账总金额
                    //已经分账金额
                    if($v['status']==1){
                        $therMoney = bcadd($therMoney,$v['fz_money'],2);
                    }
                    if($v['status']==0){
                        $noMoney = bcadd($noMoney,$v['fz_money'],2);
                    }
                }
            }
            $list = $this->model
                    ->with(['user'])
                    ->where($where)
                    //->where('api_list',1)
                   ->order($sort, $order)
                    ->paginate($limit);
            foreach ($list as $row) {
                $row->percent = $row->percent.'%';
                $row->getRelation('user')->visible(['nickname']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'sub_total_money'=>$subTotalMoney,'no_money'=>$noMoney,'ther_money'=>$therMoney);
            return json($result);
        }
        return $this->view->fetch();
    }

    //批量分账 入账
    public function multi_status($ids)
    {
        $params = $this->request->param();
        if (!empty($params['field'])){
            $updateDate[$params['field']] = 1;
            if($params['field']=="status"){
                //分账
                $updateDate["fz_time"] = time();
            }
             foreach ($ids as $id){
                 $this->model->where(['id'=>$id])->update($updateDate);
             }
            $this->success('批量操作成功');
        }
        $this->error('参数异常');
    }

    // 详情
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->assign('data',json_decode($row['sublist'],true));
        return $this->view->fetch();
    }

    // 审核
    public function check($ids)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        db('shopro_order_bill')->where('id',$ids)->update(['status'=>1,'checktime'=>date('Y-m-d H:i:s',time())]);
        $this->success('审核成功');
    }


}
